﻿using System;
using System.Collections.Generic;
using System.Data;
using DataBase;

namespace DBService
{
	public class DbRight : DbObject
	{
		public string Name
		{
			get
			{
				return this.name;
			}
			set
			{
				this.name = value;
			}
		}

		public string Description
		{
			get
			{
				return this.description;
			}
			set
			{
				this.description = value;
			}
		}

		public override string ToString()
		{
			return this.name;
		}

		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		public override bool Equals(object obj)
		{
			return obj is DbRight && ((DbRight)obj).Id == this.mId;
		}

		public DbRight(int rID, string rName, string rDesc, IDatabase rdb)
		{
			this.mDatabase = rdb;
			this.mId = rID;
			this.name = rName;
			this.description = rDesc;
		}

		public static List<DbRight> GetAllRights(IDatabase db)
		{
			List<DbRight> result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				List<DbRight> list = new List<DbRight>();
				string sql = string.Format("select Id,Name,Description from tbright", Array.Empty<object>());
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable != null)
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						list.Add(new DbRight(Convert.ToInt32(dataTable.Rows[i]["Id"]), dataTable.Rows[i]["Name"].ToString(), dataTable.Rows[i]["Description"].ToString(), db));
					}
				}
				result = list;
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public static DbRight GetRight(IDatabase db, int id)
		{
			DbRight result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				DbRight dbRight = null;
				string sql = string.Format("select Id,Name,Description from tbright where Id={0}", id);
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable != null && dataTable.Rows.Count > 0)
				{
					dbRight = new DbRight(Convert.ToInt32(dataTable.Rows[0]["Id"]), dataTable.Rows[0]["Name"].ToString(), dataTable.Rows[0]["Description"].ToString(), db);
				}
				result = dbRight;
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public static List<DbRight> GetAllRights(IDatabase db, string group)
		{
			List<DbRight> result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				List<DbRight> list = new List<DbRight>();
				string sql = string.Format("select Id,Name,Description from tbright where RightGroup='{0}'", group);
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable != null)
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						list.Add(new DbRight(Convert.ToInt32(dataTable.Rows[i]["Id"]), dataTable.Rows[i]["Name"].ToString(), dataTable.Rows[i]["Description"].ToString(), db));
					}
				}
				result = list;
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public override void RemoveFromDb()
		{
			if (base.Database.Connection.State == ConnectionState.Closed)
			{
				base.Database.Connection.Open();
			}
			string sql = string.Format("DELETE FROM tbright WHERE Id = {0}", base.Id);
			base.Database.ExecuteCommand(sql);
			if (base.Database.Connection.State == ConnectionState.Open)
			{
				base.Database.Connection.Close();
			}
		}

		public override void AddToDb(IDatabase db)
		{
			throw new NotImplementedException();
		}

		public override void UpdateToDb()
		{
			throw new NotImplementedException();
		}

		private string name;

		private string description;
	}
}
